package cn.dkdoo.utils.templateCommon.mongo;

import cn.dkdoo.utils.common.Constants;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

import static org.springframework.data.mongodb.core.query.Criteria.where;

/**
 * @author nicky
 * @date 2021/7/5 下午5:31
 */
public class DkdMongoUtils {
    /**
     * 将string集合转换成objectId
     *
     * @param collection
     * @return
     */
    public static List<ObjectId> toObjectIds(Collection<String> collection) {
        if (CollectionUtils.isEmpty(collection)) {
            return null;
        }
        List<ObjectId> res = new ArrayList<>();
        for (String col : collection) {
            if (StringUtils.isNotEmpty(col)) {
                res.add(new ObjectId(col));
            }
        }
        return res;
    }

    public static void fiterFields(Query query, String... includeFields) {
        if (includeFields == null || includeFields.length == 0) {
            return;
        }
        for (String includeField : includeFields) {
            query.fields().include(includeField);
        }
    }

    public static void addFilterShop(Criteria criteria, Integer belongType, List<String> shopIds, String shopId) {
        if (Constants.BELONG_TYPE.COMPANY.equals(belongType)) {
            criteria.and("shopId").in(shopIds);
            criteria.and("belongType").is(Constants.BELONG_TYPE.COMPANY);
        } else /*if (Constants.BELONG_TYPE.SHOP.equals(belongType))*/ {
            criteria.and("shopId").is(shopId);
            criteria.and("belongType").is(Constants.BELONG_TYPE.SHOP);
        }
    }

    public static void addFilterShop(Criteria criteria, Integer belongType, String companyId, String shopId) {
        if (Constants.BELONG_TYPE.COMPANY.equals(belongType)) {
            criteria.and("companyId").is(companyId);
            criteria.and("belongType").is(Constants.BELONG_TYPE.COMPANY);
        } else /*if (Constants.BELONG_TYPE.SHOP.equals(belongType))*/ {
            criteria.and("shopId").is(shopId);
            criteria.and("belongType").is(Constants.BELONG_TYPE.SHOP);
        }
    }

    /**
     * 范围查找
     *
     * @param fieldName
     * @param startTime
     * @param endTime
     */
    public static void addRange(Criteria criteria, String fieldName, Object startTime, Object endTime) {
        if (Objects.isNull(startTime)) {
            if (Objects.isNull(endTime)) {
                return;
            } else {
                criteria.and(fieldName).lte(endTime);
            }
        } else {
            if (Objects.isNull(endTime)) {
                criteria.and(fieldName).gte(startTime);
            } else {
                criteria.and(fieldName).gte(startTime).lte(endTime);
            }
        }
    }

    /**
     * 范围查找 左区间为闭区间->lte
     */
    public static void addRangeLeft(Criteria criteria, String fieldName, Object startTime, Object endTime) {
        if (Objects.isNull(startTime)) {
            if (Objects.isNull(endTime)) {
                return;
            } else {
                criteria.and(fieldName).lt(endTime);
            }
        } else {
            if (Objects.isNull(endTime)) {
                criteria.and(fieldName).gte(startTime);
            } else {
                criteria.and(fieldName).gte(startTime).lt(endTime);
            }
        }
    }

    /**
     * 范围查找 右区间为闭区间->gte
     */
    public static void addRangeRight(Criteria criteria, String fieldName, Object startTime, Object endTime) {
        if (Objects.isNull(startTime)) {
            if (Objects.isNull(endTime)) {
                return;
            } else {
                criteria.and(fieldName).lte(endTime);
            }
        } else {
            if (Objects.isNull(endTime)) {
                criteria.and(fieldName).gt(startTime);
            } else {
                criteria.and(fieldName).gt(startTime).lte(endTime);
            }
        }
    }

    public static void addRanges(Criteria criteria, String startFieldName, Object startTime, String endFieldName, Object endTime) {
        criteria.orOperator(
                where(startFieldName).gte(startTime).lt(endTime),
                where(endFieldName).lte(endTime).gt(startTime),
                where(startFieldName).lte(startTime).and(endFieldName).gte(endTime)
        );
    }
}
